# Load necessary libraries
library(AER)
library(ltm)
library(MASS)
library(broom)
library(psych)
library(Matching)
library(modelsummary)
library(interactions)

# Load data
data = read.csv("data.rep.csv")

#Conduct balance checks (see Online Appendix D)
print("Commitment Balance Checks")
MatchBalance(commitment ~ ideo + news + cybr.know + polt.know + coop + mili + age + is.male, data = data, , nboots = 5000)
print("Casualties Checks")
MatchBalance(casualties ~ ideo + news + cybr.know + polt.know + coop + mili + age + is.male, data = data, , nboots = 5000)
print("Dyad Balance Checks")
MatchBalance(dyad ~ ideo + news + cybr.know + polt.know + coop + mili + age + is.male, data = data, nboots = 5000)

# Cronbach Alpha results (see footnote 6 on page 12 )
print("Cooperative Cronbach Alpha")
cronbach.alpha(data.frame(data$coop.1, data$coop.2, data$coop.3, data$coop.4, data$coop.5))
print("Militant Cronbach Alpha")
cronbach.alpha(data.frame(data$mili.1, data$mili.2, data$mili.3, data$mili.4.R, data$mili.5))
print("Isolationist Cronbach Alpha")
cronbach.alpha(data.frame(data$isol.1, data$isol.2, data$isol.3.R, data$isol.4, data$isol.5))

# Descriptive Statistics (see page 12)
print("Age Descriptive Statistics")
describe(data$age)
print("Male Participant Percentage")
prop.table(table(data$is.male))
print("Education Percentage")
prop.table(table(data$educ)) 
print("News Descriptive Statistics")
describe(data$news)
print("Ideology Descriptive Statistics")
describe(data$ideo) 
print("Cooperative Descriptive Statistics")
describe(data$coop)
print("Militant Descriptive Statistics")
describe(data$mili)
print("Isolationist Descriptive Statistics")
describe(data$isol)
print("Cyber Knowledge Descriptive Statistics")
describe(data$cybr.know)
print("Political Knowledge Descriptive Statistics")
describe(data$polt.know)
print("Cyber Response Outcome Descriptive Statistics")
describe(data$resp.cybr)
print("Conventional Response Outcome Descriptive Statistics")
describe(data$resp.conv)

print("T-Test Between Outcome Means")
t.test(data$resp.cybr, data$resp.conv, alternative = "two.sided") #(see pages 12 - 13)

# Causal Analysis 
# Main Effects (see pages 13 - 16)
print("Cyber Means Regression Table")
summary(lm(resp.cybr ~ commitment + casualties + dyad + ideo.c + news.c + cybr.know.c + polt.know.c + coop.c + is.male + age.c, data = data))
print("Conventional Means Regression Table")
summary(lm(resp.conv ~ commitment + casualties + dyad + ideo.c + news.c + cybr.know.c + polt.know.c + coop.c + is.male + age.c, data = data))
# Main Effects Robustness (see Online Appendix B)
print("Cyber Means Regression Table Robustness Check")
tidy(polr(as.factor(resp.cybr) ~ commitment + casualties + dyad + ideo.c + news.c + cybr.know.c + polt.know.c + coop.c + is.male + age.c, data = data, Hess = TRUE), p.values = TRUE)
print("Conventional Means Regression Table Robustness Check")
tidy(polr(as.factor(resp.conv) ~ commitment + casualties + dyad + ideo.c + news.c + cybr.know.c + polt.know.c + coop.c + is.male + age.c, data = data, Hess = TRUE), p.values = TRUE)
# Interaction Table and Plot (see page 14 - 15 and Online Appenix C)
resp.cybrx.mod = lm(resp.cybr ~ commitment*cybr.know.c + casualties*cybr.know.c + dyad*cybr.know.c + ideo.c + news.c + polt.know.c + coop.c + is.male + age.c, data = data)
resp.convx.mod = lm(resp.conv ~ commitment*cybr.know.c + casualties*cybr.know.c + dyad*cybr.know.c + ideo.c + news.c + polt.know.c + coop.c + is.male + age.c, data = data)
print("Cyber Means Regression Table Interactions")
summary(resp.cybrx.mod) 
print("Conventional Means Regression Table Interactions")
summary(resp.convx.mod)
print("Confidence Interval for interaction between Commitment and Cyber Knowledge affecting Cyber Response")
confint(resp.cybrx.mod,"commitment:cybr.know.c", level=0.95)
print("Confidence Interval for interaction between Commitment and Cyber Knowledge affecting Conventional Response")
confint(resp.convx.mod,"commitment:cybr.know.c", level=0.95)
print("Confidence Interval for interaction between Casualties and Cyber Knowledge affecting Cyber Response")
confint(resp.cybrx.mod,"cybr.know.c:casualties", level=0.95)
#See page 14
interact_plot(resp.cybrx.mod , pred = commitment, modx = cybr.know.c, x.label = "Commitment", y.label = "Cyber Response", legend.main = "Expertise")
interact_plot(resp.convx.mod , pred = commitment, modx = cybr.know.c, x.label = "Commitment", y.label = "Conventional Response", legend.main = "Expertise")
#See page 15
interact_plot(resp.cybrx.mod , pred = casualties, modx = cybr.know.c, x.label = "Casualties", y.label = "Cyber Response", legend.main = "Expertise")
interact_plot(resp.convx.mod , pred = casualties, modx = cybr.know.c, x.label = "Casualties", y.label = "Conventional Response", legend.main = "Expertise")

